package com.tkjt.pms.modules.report.service.impl;

import java.util.*;

import com.tkjt.pms.common.utils.EsDateUtils;
import com.tkjt.pms.modules.common.mapper.PMSCommonMapper;
import com.tkjt.pms.modules.kpiadmin.mapper.DataGridMapper;
import com.tkjt.pms.modules.report.service.common.ExcelBuilderUtils;
import com.tkjt.pms.modules.report.util.DeptConstant;
import com.tkjt.pms.server.common.DepartUserChangeService;
import org.apache.commons.lang.StringUtils;
import org.jeecg.modules.system.mapper.SysUserMapper;
import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tkjt.pms.common.entity.TkIndexInfo;
import com.tkjt.pms.common.entity.TkIndexValue;
import com.tkjt.pms.common.entity.TkIndexValueMirror;
import com.tkjt.pms.modules.common.mapper.TkIndexValueMapper;
import com.tkjt.pms.modules.common.mapper.TkIndexValueMirrorMapper;
import com.tkjt.pms.modules.report.mapper.ReportDownLoadMapper;
import com.tkjt.pms.modules.report.service.IndexDataLoader;
import com.tkjt.pms.modules.report.vo.DatagridVO;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class IndexDataLoaderImpl implements IndexDataLoader {

	@Autowired
	private DataGridMapper dtaGridMapper;
	@Autowired
	private TkIndexValueMapper tkIndexValueMapper;
	@Autowired
	private TkIndexValueMirrorMapper tkIndexValueMirrorMapper;
	@Autowired
	private ReportDownLoadMapper reportDownLoadMapper;
	@Autowired
	private SysUserMapper sysUserMapper;
	@Autowired
	private DepartUserChangeService departUserChangeService;
	@Override
	public Map<String, List<TkIndexInfo>> loadIndexs(DatagridVO datagridVO,boolean isTotal) {
		log.info("loadIndexs......");
		Map<String,Object> param = new HashMap<>();
		param.put("indexGroup",datagridVO.getIndexGroup());
		param.put("indexCodes",datagridVO.getIndexCodes());
		datagridVO.setTotalShow(isTotal);
		List<TkIndexInfo> infos = reportDownLoadMapper.selectList(datagridVO);
		List<String> indexCodes = new ArrayList<>();
		Map<String, List<TkIndexInfo>> infoMaps = new LinkedHashMap<>() ;
		for(TkIndexInfo info : infos){
			if(indexCodes.size() == 0 || !indexCodes.contains(info.getIndexCode())){
				List<TkIndexInfo> infoChilds = new ArrayList<>();
				infoChilds.add(info);
				infoMaps.put(info.getIndexCode(),infoChilds);
				indexCodes.add(info.getIndexCode());
			}else{
				List<TkIndexInfo> infoChilds = infoMaps.get(info.getIndexCode());
				infoChilds.add(info);
				infoMaps.put(info.getIndexCode(),infoChilds);
			}
		}
		log.info("loadIndexs end.");
		return infoMaps;
	}

	@Override
	public Map<String, String> loadIndexWeights(String indexYearMonth, String indexGroup, String indexCode, List<String> departCodes) {
		Map<String,Object> param = new HashMap<>();
		param.put("month","WEIGHT_M"+indexYearMonth.substring(4));
		param.put("indexCode",indexCode);
		param.put("departCodes",departCodes);
		List<Map<String, Object>> list = reportDownLoadMapper.getWeightByParam(param);
		Map<String,String> WeightMap = new HashMap<>();
		for(Map<String,Object> item : list){
			WeightMap.put(item.get("orgCode").toString(),item.get("monthValue").toString());
		}
		return WeightMap;
	}

	@Override
	public TkIndexValue loadIndexValues(String indexYearMonth, String indexValueCode, String indexDepart, String indexScorer,String dataType) {
		QueryWrapper<TkIndexValue> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(TkIndexValue::getDataType,dataType);
		queryWrapper.lambda().eq(TkIndexValue::getIndexValueCode,indexValueCode);
		queryWrapper.lambda().eq(TkIndexValue::getIndexDepart,indexDepart);
		if(StringUtils.isNotEmpty(indexScorer)){
			queryWrapper.lambda().eq(TkIndexValue::getIndexScorer,indexScorer);
		}
		String[] roleCodes = {"ykgm","ykkpiowner"};
		List<String> userNames = sysUserMapper.selectUserNameByRoleCodes(roleCodes);
		userNames.add("system");
		queryWrapper.lambda().in(TkIndexValue::getIndexExecutor, userNames);
		queryWrapper.lambda().orderByDesc(TkIndexValue::getUpdateDate);
		List<TkIndexValue> list = tkIndexValueMapper.selectList(queryWrapper);
		if(list.size() == 0){
			return null;
		}else{
			return list.get(0);
		}

	}

	@Override
	public TkIndexValueMirror loadMirrorIndexValues(String indexYearMonth, String indexValueCode, String indexDepart, String indexScorer,String dataType) {
		QueryWrapper<TkIndexValueMirror> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(TkIndexValueMirror::getMirrorCode,"MC"+indexYearMonth);
		queryWrapper.lambda().eq(TkIndexValueMirror::getDataType,dataType);
		queryWrapper.lambda().eq(TkIndexValueMirror::getIndexValueCode,indexValueCode);
		queryWrapper.lambda().eq(TkIndexValueMirror::getIndexDepart,indexDepart);
		if(StringUtils.isNotEmpty(indexScorer)){
			queryWrapper.lambda().eq(TkIndexValueMirror::getIndexScorer,indexScorer);
		}
		String[] roleCodes = {"ykgm","ykkpiowner"};
		List<String> userNames = sysUserMapper.selectUserNameByRoleCodes(roleCodes);
		userNames.add("system");
		queryWrapper.lambda().in(TkIndexValueMirror::getIndexExecutor, userNames);
		queryWrapper.lambda().orderByDesc(TkIndexValueMirror::getUpdateDate);
		List<TkIndexValueMirror> list = tkIndexValueMirrorMapper.selectList(queryWrapper);
		if(list.size() == 0){
			return null;
		}else{
			return list.get(0);
		}

	}

	@Override
	public Map<String, Object> loadMirroIndexValues(String indexFormat,String indexYearMonth, String indexValueCode, String indexDepart, String indexScorer,String titleName,String dataType) {
		/*QueryWrapper<TkIndexValueMirror> queryWrapper = new QueryWrapper<>();
		//queryWrapper.lambda().eq(TkIndexValueMirror::getMirrorCode,"MC"+indexYearMonth);
		queryWrapper.lambda().eq(TkIndexValueMirror::getDataType,dataType);
		queryWrapper.lambda().eq(TkIndexValueMirror::getIndexValueCode,indexValueCode);
		queryWrapper.lambda().eq(TkIndexValueMirror::getIndexDepart,indexDepart);
		if(StringUtils.isNotEmpty(indexScorer)){
			queryWrapper.lambda().eq(TkIndexValueMirror::getIndexScorer,indexScorer);
		}
		String[] roleCodes = {"ykgm","ykkpiowner"};
		List<String> userNames = sysUserMapper.selectUserNameByRoleCodes(roleCodes);
		userNames.add("system");
		queryWrapper.lambda().in(TkIndexValueMirror::getIndexExecutor, userNames);
		queryWrapper.lambda().orderByDesc(TkIndexValueMirror::getUpdateDate);
		List<TkIndexValueMirror> list = tkIndexValueMirrorMapper.selectList(queryWrapper);*/
		Map<String,Object> data = new HashMap<>();
		List<String> columnKeys = ExcelBuilderUtils.getColumnsMKeys();
		Map<String,Object> param = new HashMap<>();
		param.put("dataType",dataType);
		param.put("indexValueCode",indexValueCode);
		param.put("indexDepart",indexDepart);
		String[] roleCodes = {"ykgm","ykkpiowner"};
		List<String> userNames = sysUserMapper.selectUserNameByRoleCodes(roleCodes);
		userNames.add("system");
		param.put("indexExecutors",userNames);
		param.put("month","m_total");
		for(String key : columnKeys) {
			Map<String,Object> changeData = departUserChangeService.changeUserForMyReprot(indexYearMonth.substring(0,4)+key.substring(1),indexDepart+"@"+indexScorer,titleName);
			if(changeData == null){
				data.put(key, "-");
			}else{
				param.put("mirrorCode","MC"+indexYearMonth.substring(0,4)+key.substring(1));
				param.put("indexScorer",changeData.get("beforeUserCode"));
				Map<String,Object> value = dtaGridMapper.getMirrorMonthIndexValueForParam(param);
				if(value == null){
					data.put(key, "-");
				}else{
					data.put(key, DeptConstant.getPercent(Double.valueOf(value.get("m_total").toString()),indexFormat));

				}
			}
		}
		return data;
	}

}
